﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Home.Master" Inherits="System.Web.Mvc.ViewPage" %>
<%@ Import Namespace="WebHoiChoMuaBan.Models"  %>
<%@ Import Namespace="WebHoiChoMuaBan.Controllers.Helpers" %>
<%@ Import Namespace="WebHoiChoMuaBan.Controllers.Utility" %>

<asp:Content ID="Content2" ContentPlaceHolderID="TitleHolder" runat="server">
    Rao vặt sản phẩm
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="HeadHolder" runat="server">
</asp:Content>
<%--Minh Thêm--%>
<%--List Menu Header--%>
<%--copy từ maincontent lên ListMenu--%>
<asp:Content ID="Content4" ContentPlaceHolderID="ListMenuHeader" runat="server">
  <!-- Group list menu -->
    <%--    <div class="hoicho_group_product">
            <div class="hoicho_header_feature">
                <ul>
                <%
                    string bold = "style='font-weight: bold'";
                    PageType curPage = (PageType)ViewData["CurrentPage"];
                    Category curCate = (Category)ViewData["CurrentCategory"];
                    int needSell   = (int)ViewData["NeedSell"];
                    int provinceId = (int)ViewData["ProvinceId"];
                    
                    List<Category> cate = (List<Category>)ViewData["Categories"];
                    int i;
                    foreach(Category c in cate)
                    {
                        bool isCurrent = curCate != null && 
                                            ((curCate.ParentCategory != null && c.CateId == curCate.ParentCategory.CateId) || 
                                             c.CateId == curCate.CateId);
                        //Response.Write(string.Format("<li class='page_{3} {0}'><a href='#' onclicked='switchTab({3}, this); return false;' rel='cate_{2}' >{1}</a></li>", ((curCate != null && c.CateId == curCate.ParentCategory.CateId) || (curCate == null && i == 1) ? "selected_menu" : string.Empty), c.CateName, c.CateId, c.PageId));
                        Response.Write(string.Format("<li class='{0}'><a href='{1}?pid={2}&cid={3}&p={4}&need={5}&prvid={6}'>{7}</a></li>", 
                                                    (isCurrent ? "selected_menu" : string.Empty), 
                                                    Url.Action("Page", "Home", new { pageName = MiscUtil.EscapeString(curPage.PageName) }),
                                                    curPage.PageId,
                                                    c.CateId,
                                                    1,
                                                    needSell,
                                                    provinceId,
                                                    c.CateName));
                    }
                 %>
                </ul>
            </div>
            <div class="hoicho_feature_content">
                <div class="private_breakpage_replycoment col_left reset_margin">
                <%
                    Response.Write(string.Format("<a {0} href='{2}?pid={3}&cid={4}&p={5}&need={6}&prvid={8}'>Cần bán</a> | <a {1} href='{2}?pid={3}&cid={4}&p={5}&need={7}&prvid={8}'>Cần mua</a>",
                                                    (needSell == NewsProductHelper.NEED_SELL ? bold : string.Empty),
                                                    (needSell == NewsProductHelper.NEED_BUY ? bold : string.Empty),
                                                    Url.Action("Page", "Home", new { pageName = MiscUtil.EscapeString(curPage.PageName) }),
                                                    curPage.PageId,
                                                    (curCate != null ? curCate.CateId : 0),
                                                    1,
                                                    NewsProductHelper.NEED_SELL,
                                                    NewsProductHelper.NEED_BUY,
                                                    provinceId));
                %>
                </div>
                <div class="hoicho_select_province col_right">
                    <span class="hoicho_margin_right">
                        <%
                            StringBuilder provLinks = new StringBuilder();
                            provLinks.Append("<a {0} href='{4}?pid={5}&cid={6}&p={7}&need={8}&prvid=2'>Hà Nội</a> | ");
                            provLinks.Append("<a {1} href='{4}?pid={5}&cid={6}&p={7}&need={8}&prvid=6'>Hải Phòng</a> | ");
                            provLinks.Append("<a {2} href='{4}?pid={5}&cid={6}&p={7}&need={8}&prvid=3'>Hồ Chí Minh</a> | ");
                            provLinks.Append("<a {3} href='{4}?pid={5}&cid={6}&p={7}&need={8}&prvid=5'>Đà Nẵng</a> |");
                            Response.Write(string.Format(provLinks.ToString(),
                                                    (provinceId == 2 ? bold : string.Empty), // {0}
                                                    (provinceId == 6 ? bold : string.Empty), // {1}
                                                    (provinceId == 3 ? bold : string.Empty), // {2}
                                                    (provinceId == 5 ? bold : string.Empty), // {3}
                                                    Url.Action("Page", "Home", new { pageName = MiscUtil.EscapeString(curPage.PageName) }),
                                                    curPage.PageId,
                                                    (curCate != null ? curCate.CateId : 0),
                                                    1,
                                                    needSell // buy or sell
                                                    ));
                         %>
                    </span>
                    <span>
                        <select id="cbbProvince" class="hoicho_input_select_province">
                            <%
                                string selected = "selected='selected'";
                                List<Province> provinces = (List<Province>)ViewData["Provinces"];
                                foreach(Province pr in provinces) {
                                    Response.Write(string.Format("<option value='{0}' {2}>{1}</option>", 
                                                                pr.ProvId, 
                                                                pr.ProvName,
                                                                (provinceId == pr.ProvId ? selected : string.Empty)
                                                                ));
                                }
                            %>
                        </select>
                        <script type="text/javascript">
                            jQuery("#cbbProvince").change(function () {
                                location.href=<%=string.Format("'{0}?pid={1}&cid={2}&p={3}&need={4}'",
                                                    Url.Action("Page", "Home", new { pageName = MiscUtil.EscapeString(curPage.PageName) }),
                                                    curPage.PageId,
                                                    (curCate != null ? curCate.CateId : 0),
                                                    1,
                                                    needSell // buy or sell
                                                    ) %> + '&prvid=' + jQuery(this).val();
                            });
                        </script>
                    </span>
                </div>
            </div>
            
                <%
                    //int j = 1;
                    //foreach (Category c in cate)
                    {
                        //Response.Write(string.Format("<div id='cate_{1}' class='hoicho_group_listmenu_fation page_{2}' {0}>", (j>1 ? "style='display:none'" : string.Empty), c.CateId, c.PageId));
                        Response.Write("<div>");
                        //j++;
                        
                        if (curCate != null)
                        {
                            List<CategoryHelper.SubCategory> subcategories = null;
                            if (curCate.SubCategories != null && curCate.SubCategories.Count > 0)
                            {
                                //Số dòng mỗi cột
                                subcategories = (from sub in curCate.SubCategories
                                                 select new CategoryHelper.SubCategory
                                                 {
                                                     id = sub.CateId,
                                                     name = sub.CateName
                                                 }).ToList();
                            }
                            else if (curCate.ParentCategory != null)
                            {
                                subcategories = (from sub in curCate.ParentCategory.SubCategories
                                                 select new CategoryHelper.SubCategory
                                                 {
                                                     id = sub.CateId,
                                                     name = sub.CateName
                                                 }).ToList();
                            }

                            if (subcategories != null)
                            {
                                int perRow = (int)Math.Ceiling((double)subcategories.Count / 4);

                                //4 cột subcategories
                                for (i = 0; i < 4; i++)
                                {
                                    var subCate = (from sub in subcategories select sub).Skip(perRow * i).Take(perRow);
                                    if (subCate != null)
                                    {
                                        Response.Write("<div class='private_menu_list_product'><ul>");
                                        foreach (var s in subCate)
                                            Response.Write(string.Format("<li><a href='{0}?pid={1}&cid={2}&p={3}' style='font-weight: {4}'>{5}</a></li>",
                                                                            Url.Action("Page", "Home", new { pageName = MiscUtil.EscapeString(curPage.PageName) }),
                                                                            curPage.PageId,
                                                                            s.id,
                                                                            1,
                                                                            ((curCate != null && curCate.CateId == s.id) ? "bold" : "normal"),
                                                                            s.name));
                                        Response.Write("</ul></div>");
                                    }
                                }
                            }
                        }
                        Response.Write("</div>");
                    }
                 %>
        </div>--%>
        <!-- End Group list menu -->
</asp:Content>
<%--end List Menu Header--%>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContentHolder" runat="server">
        <!-- Group list menu -->
        <%--<div class="hoicho_group_product">
            <div class="hoicho_header_feature">
                <ul>
                <%
                    PageType curPage = (PageType)ViewData["CurrentPage"];
                    Category curCate = (Category)ViewData["Category"];
                    
                    List<Category> cate = (List<Category>)ViewData["SubCategories"];
                    int i;
                    foreach(Category c in cate)
                    {
                        bool isCurrent = curCate != null && 
                                            ((curCate.ParentCategory != null && c.CateId == curCate.ParentCategory.CateId) || 
                                             c.CateId == curCate.CateId);
                        //Response.Write(string.Format("<li class='page_{3} {0}'><a href='#' onclicked='switchTab({3}, this); return false;' rel='cate_{2}' >{1}</a></li>", ((curCate != null && c.CateId == curCate.ParentCategory.CateId) || (curCate == null && i == 1) ? "selected_menu" : string.Empty), c.CateName, c.CateId, c.PageId));
                        Response.Write(string.Format("<li class='{0}'><a href='{1}?pid={2}&cid={3}&p={4}'>{5}</a></li>", 
                                                    (isCurrent ? "selected_menu" : string.Empty), 
                                                    Url.Action("Page", "Home", new { pageName = MiscUtil.EscapeString(curPage.PageName) }),
                                                    curPage.PageId,
                                                    c.CateId,
                                                    1,
                                                    c.CateName));
                    }
                 %>
                </ul>
            </div>
            <div class="hoicho_feature_content">
                <div class="private_breakpage_replycoment col_left reset_margin">
                    <a href="#" class="selected_menu">Cần mua</a> | <a href="#">Cần bán</a>
                </div>
                <div class="hoicho_select_province col_right">
                    <span class="hoicho_margin_right">
                        <a href="#">Hà nội</a> | 
                        <a href="#">Hải phòng</a> | 
                        <a href="#">Hồ chí minh</a> | 
                        <a href="#">Đà nẵng</a> | 
                    </span>
                    <span>
                        <select id="Select1" class="hoicho_input_select_province">
                            <option>Tỉnh khác</option>
                        </select>
                    </span>
                </div>
            </div>
            
                <%
                    //int j = 1;
                    //foreach (Category c in cate)
                    {
                        //Response.Write(string.Format("<div id='cate_{1}' class='hoicho_group_listmenu_fation page_{2}' {0}>", (j>1 ? "style='display:none'" : string.Empty), c.CateId, c.PageId));
                        Response.Write("<div>");
                        //j++;
                        
                        if (curCate != null)
                        {
                            List<CategoryHelper.SubCategory> subcategories = null;
                            if (curCate.SubCategories != null && curCate.SubCategories.Count > 0)
                            {
                                //Số dòng mỗi cột
                                subcategories = (from sub in curCate.SubCategories
                                                 select new CategoryHelper.SubCategory
                                                 {
                                                     id = sub.CateId,
                                                     name = sub.CateName
                                                 }).ToList();
                            }
                            else
                            {
                                subcategories = (from sub in curCate.ParentCategory.SubCategories
                                                 select new CategoryHelper.SubCategory
                                                 {
                                                     id = sub.CateId,
                                                     name = sub.CateName
                                                 }).ToList();
                            }
                            int perRow = (int)Math.Ceiling((double)subcategories.Count / 4);

                            //4 cột subcategories
                            for (i = 0; i < 4; i++)
                            {
                                var subCate = (from sub in subcategories select sub).Skip(perRow * i).Take(perRow);
                                if (subCate != null)
                                {
                                    Response.Write("<div class='private_menu_list_product'><ul>");
                                    foreach (var s in subCate)
                                        Response.Write(string.Format("<li><a href='{0}?pid={1}&cid={2}&p={3}' style='font-weight: {4}'>{5}</a></li>",
                                                                        Url.Action("Page", "Home", new { pageName = MiscUtil.EscapeString(curPage.PageName) }),
                                                                        curPage.PageId,
                                                                        s.id,
                                                                        1,
                                                                        ((curCate != null && curCate.CateId == s.id) ? "bold" : "normal"),
                                                                        s.name));
                                    Response.Write("</ul></div>");
                                }
                            }
                        }
                        Response.Write("</div>");
                    }
                 %>
        </div>--%>
        <!-- End Group list menu -->
        <!-- Group san pam noi bat -->
         <!-- Group san pam noi bat -->
        <div class="hoicho_group_product">
            <div class="hoicho_top_groupproduct">
                <p class="hoicho_texttop_product">
                    Sản phẩm nổi bật</p>
            </div>
            <div class="hoicho_group_product_content">
                <%
                    NewsProductHelper.FetchProductResult lstHot = (NewsProductHelper.FetchProductResult)ViewData["HotProducts"];
                   int i=1; 
                    foreach (var hot in lstHot.products)
                    {
                        if( i++ != 5 )
                            Response.Write("<div class='hoicho_product_thumb'>");
                        else
                            Response.Write("<div class='hoicho_product_thumb reset_margin'>");
                     %>
                <!-- Product thumb -->
                
                    <% 
                        
                        string link = string.Format("<a href='{0}?prid={1}'>",
                                            Url.Action("Detail", "NewsProduct", new { productTitle = MiscUtil.EscapeString(hot.NProTitle) }),
                                            hot.NProId                                            
                                            );
                    %>
                    <%=link %>                    
                    <img src="/../Content/Images/Upload/<%=(hot.NewsProductThumbs != null && hot.NewsProductThumbs.Count > 0 ? hot.NewsProductThumbs.First().NProThumbName : "" )%>" width="94" height="80" alt="" class="hoicho_img_productthumb" />
                    </a>
                    <p class="font_bold">
                        <%=link %>
                        
                        <%//Minh Thêm
                        /*-----------Truncate Product Title----------------*/
                        /*Thiết lập Title = 40 chars*/
                        if (hot.NProTitle.Length > 40)
                        {
                            hot.NProTitle = hot.NProTitle.Substring(0, 37) + "...";
                        }
                        else
                        {
                            int iCount = 40 - hot.NProTitle.Length;
                            for (int k = 0; k < iCount; k++)
                            {
                                hot.NProTitle += " ";
                            }
                        }
                        /*-----------End truncate Product Title*/ %>
                            <%=hot.NProTitle %>
                        </a></p>
                    
                    <br />
                    <p>
                    <%
                         //Minh Thêm
                        /*-----------Truncate Product Provice Name----------------*/
                        /*Thiết lập Product Provice Name = 10 chars*/
                        if (hot.Province.ProvName.Length > 10)
                        {
                            hot.Province.ProvName = hot.Province.ProvName.Substring(0, 7) + "...";
                        }
                         /*-----------End truncate Product Provice Name*/ %>
                           
                        Nơi rao: <span class="hoicho_ograne_color"><%=hot.Province.ProvName %></span></p>
                    <p>
                        Ngày đăng: <span class="hoicho_ograne_color"><%=hot.NProDate.ToString("dd/MM/yyyy") %></span></p>
                    <div class="hoicho_bt_product_price">
                        <%=(hot.NProPrice == null ? "Thương lượng" : hot.NProPrice.Value + " " + hot.NProPriceUnit) %></div>
                    <p>
                        <img src="/../Content/Images/hoicho_icon_view.gif" alt="" /><span class="hoicho_margin_leftright">Lượt
                            xem:</span><span class="hoicho_margin_leftright hoicho_ograne_color"><%=hot.NProViewCount %></span></p>
                    <p>
                <img src="/../Content/Images/hoicho_icon_savenews.gif" alt="" class="hoicho_margin_leftright" /><a
                    href="#" class="SaveButton" onclick="SaveNews(<%=hot.NProId %>)" data-url='<%=hot.NProId %>'>Lưu tin</a></p>
                <%
                        Response.Write("</div>");
                    }
                 %>
                <!-- End Product thumb -->
            </div>
        </div>
        <!-- End Group san pam noi bat -->
        <!-- End Group san pam noi bat -->
        <a name="noibat"></a>
        <div class="private_breakpage" style="text-align: right">
            <%
                PageType curPage = (PageType)ViewData["CurrentPage"];
                Category curCate = (Category)ViewData["CurrentCategory"];
                NewsProductHelper.FetchProductResult lstNews = (NewsProductHelper.FetchProductResult)ViewData["NewProducts"];
                int p;
                if (Request.QueryString["p"] != null && int.TryParse(Request.QueryString["p"], out p))
                {
                 %>
            <% if (p > 1) { %><a href='<%=string.Format("{0}?p={1}#noibat'",
                                                    Url.Action("ViewSaved", "Home"),
                                                    p-1) %>'><img src="/../Content/Images/arrow_breakpage_left.gif" alt="">&nbsp;Trang trước</a> <% } %>
            <%
                if (p > 3)
                    Response.Write(string.Format("<a href='{0}?p={1}#noibat' {2}>{3}</a> ",
                                                    Url.Action("ViewSaved", "Home"),
                                                    1,
                                                    string.Empty,
                                                     1));
                if (p > 4)
                    Response.Write("...");
                int total = lstNews.totalPages;
                int from = (p - 2 < 1 ? 1 : p - 2);
                int to = (p + 2 >= total ? total : p + 2);
                for (i = from; i <= to; i++)
                    Response.Write(string.Format("<a href='{0}?p={1}#noibat' {2}>{3}</a> ",
                                                    Url.Action("ViewSaved", "Home"),
                                                    i, 
                                                    (i==p ? "class='menu_select'" : string.Empty),
                                                     i));
                if (p <= total - 4)
                    Response.Write("...");
                if (p <= total - 3)
                    Response.Write(string.Format("<a href='{0}?p={1}#noibat' {2}>{3}</a> ",
                                                    Url.Action("ViewSaved", "Home"),
                                                    total,
                                                    string.Empty,
                                                    total));
             %>
            
            <% if (p < lstNews.totalPages) { %><a href='<%=string.Format("{0}?p={1}#noibat'",
                                                    Url.Action("ViewSaved", "Home"),
                                                    p+1) %>'>Trang sau&nbsp;<img src="/../Content/Images/arrow_breakpage_right.gif" alt=""></a> <% } %>
            <% }  %>
        </div>
        <%
            i = 0;
            foreach (NewsProduct n in lstNews.products)
            {
         %>
            <div class="private_thumb_listpr">
                <div class="private_img_post_bt">
                    <a href="hoicho_detail_sp.html">
                        <img class="private_img_post hoicho_margin_topbottom" 
                            src="/../Content/Images/Upload/<%=(n.NewsProductThumbs != null && n.NewsProductThumbs.Count > 0 ? n.NewsProductThumbs.First().NProThumbName : "" )%>" 
                            width="140" height="101" /></a></div>
                <div class="private_post_text">
                    <p class="private_text_title">
                        <a href="#"><%=n.NProTitle %></a>
                    </p>
                <p class="hoicho_margin_topbottom">
                    <%=n.NProIntro%>
                </p>
                <p class="hoicho_margin_topbottom">
                    <a href="#"><%=n.Member.MemUsername %></a> <span class="hoicho_timer_rate">- <%=n.Province.ProvName %> - <%=n.NProDate.ToString("dd/MM/yyyy hh:mm") %></span></p>
                <div class="private_friend_chat bg_reset">
                    <span class="hoicho_margin_right">
                        <strong>Giá bán:&nbsp;</strong>
                        <span class="private_price"><%=(n.NProPrice == null ? "Thương lượng" : n.NProPrice.Value + " " + n.NProPriceUnit) %></span>
                    </span>
                </div>
            </div>
            <div class="hoicho_list_reviews">
                <div>
                    <input type="submit" style="#width: 100px;" id="Submit1" value="Đánh giá" class="private_bt_submit_news_disable">
                </div>
                <div>
                    <a href="#" class="hoicho_margin_topbottom">
                        <img alt="" src="/../Content/Images/button_member.gif"></a></div>
                        <div >
                          <a href="#" class="hoicho_bt_action" onclick="SaveNews(<%=n.NProId %>)">
                            <img class="hoicho_bt_action_image" src="/../Content/Images/icon_save.gif" align="bottom" />
                            Lưu tin </a>
                        </div>
            </div>
        </div>
        <!-- End thumb list sp -->
        <%
            }
        %>
        <div class="private_breakpage" style="text-align: right">
            
            <%
                if (Request.QueryString["p"] != null && int.TryParse(Request.QueryString["p"], out p))
                {
                 %>
            <% if (p > 1) { %><a href='<%=string.Format("{0}?p={1}#noibat'",
                                                    Url.Action("ViewSaved", "Home"),
                                                    p-1) %>'><img src="/../Content/Images/arrow_breakpage_left.gif" alt="">&nbsp;Trang trước</a> <% } %>
            <%
                if (p > 3)
                    Response.Write(string.Format("<a href='{0}?p={1}#noibat' {2}>{3}</a> ",
                                                    Url.Action("ViewSaved", "Home"),
                                                    1,
                                                    string.Empty,
                                                     1));
                if (p > 4)
                    Response.Write("...");
                int total = lstNews.totalPages;
                int from = (p - 2 < 1 ? 1 : p - 2);
                int to = (p + 2 >= total ? total : p + 2);
                for (i = from; i <= to; i++)
                    Response.Write(string.Format("<a href='{0}?p={3}#noibat' {2}>{3}</a> ",
                                                    Url.Action("ViewSaved", "Home"),
                                                    i, 
                                                    (i==p ? "class='menu_select'" : string.Empty),
                                                     i));
                if (p <= total - 4)
                    Response.Write("...");
                if (p <= total - 3)
                    Response.Write(string.Format("<a href='{0}?p={1}#noibat' {2}>{3}</a> ",
                                                    Url.Action("ViewSaved", "Home"),
                                                    total,
                                                    string.Empty,
                                                    total));
             %>
            
            <% if (p < lstNews.totalPages) { %><a href='<%=string.Format("{0}?p={1}#noibat'",
                                                    Url.Action("ViewSaved", "Home"),
                                                    p+1) %>'>Trang sau&nbsp;<img src="/../Content/Images/arrow_breakpage_right.gif" alt=""></a> <% } %>
            <% }  %>
        </div>
</asp:Content>
